package com.xinzhi.shop;
import com.xinzhi.shop.constant.ShopConstant;
import com.xinzhi.shop.util.ScannerUtil;
import java.util.List;
import java.util.Scanner;
/**
 * @author zn
 * @date 2020/2/18
 **/
public class Shop {
    //商场的消费者，难点
    private User consumer = new User();
    public static void main(String[] args) {
        Shop shop = new Shop();
//展示登录注册页面
        shop.loginView();
//展示商城购物的页面
        shop.shopView();
    }
    //展示购物页面
    private void shopView() {
        System.out.println("请根据提示选择相应的功能：");
        boolean flag = true;
        while (flag) {
            System.out.println(
                    ShopConstant.GOODS_TYPE_NECESSITIES + "：日用品 " +
                            ShopConstant.GOODS_TYPE_CRUET + "：调味品 " +
                            ShopConstant.GOODS_TYPE_FOOD + "：食品 " +
                            ShopConstant.FUNCTION_LOOK_CART + "：查看购物车 " +
                            ShopConstant.FUNCTION_PAY_MONEY + "：支付 " +
                            ShopConstant.FUNCTION_RECHARGE + "：充值 " +
                            ShopConstant.FUNCTION_ACCOUNT_MANAGER + "：用户信息 " +
                            ShopConstant.FUNCTION_EXIT + "：退出 "
            );
            String function = ScannerUtil.getInput();
            if (ShopConstant.GOODS_TYPE_NECESSITIES.equals(function)) {
                this.showGoodsMessage(ShopConstant.GOODS_TYPE_NECESSITIES);
            } else if (ShopConstant.GOODS_TYPE_CRUET.equals(function)) {
                this.showGoodsMessage(ShopConstant.GOODS_TYPE_CRUET);
            } else if (ShopConstant.GOODS_TYPE_FOOD.equals(function)) {
                this.showGoodsMessage(ShopConstant.GOODS_TYPE_FOOD);
            } else if (ShopConstant.FUNCTION_LOOK_CART.equals(function)) {
                consumer.lookAtCart();
            } else if (ShopConstant.FUNCTION_PAY_MONEY.equals(function)) {
                consumer.pay();
            } else if (ShopConstant.FUNCTION_RECHARGE.equals(function)) {
                consumer.recharge();
            } else if (ShopConstant.FUNCTION_ACCOUNT_MANAGER.equals(function)) {
                consumer.showUserManger();
            } else if (ShopConstant.FUNCTION_EXIT.equals(function)) {
                System.exit(-1);
            }
        }
    }
    //展示商品信息
    private void showGoodsMessage(String type){
        List<Goods> goodsByType = Goods.getGoodsByType(type);
        String str = "";
        for (Goods goods : goodsByType) {
            str = str + "商品编号：" + goods.getId() +
                    " - 商品名称：" + goods.getName() +
                    " - 商品价格："+ goods.getPrice() +"\n";
        }
        System.out.println(str);
        System.out.print("请输入要购买的商品编号：");
        String goodsId = ScannerUtil.getInput();
        System.out.print("请输入要购买的商品数量：");
        String goodsCount = ScannerUtil.getInput();
        consumer.addGoodsToCart(goodsId,Integer.parseInt(goodsCount));
    }
    //展示注册登录页面
    private void loginView() {
        boolean flag = true;
        while (flag) {
            System.out.println("请选择功能:" + ShopConstant.USER_LOGIN + "：登录 "
                    +
                    ShopConstant.USER_REGIST + "：注册");
            Scanner scanner = new Scanner(System.in);
            String function = scanner.next();
            if (ShopConstant.USER_LOGIN.equals(function)) {
                flag = !consumer.login();
            } else if (ShopConstant.USER_REGIST.equals(function)) {
                consumer.regist();
            }
        }
    }
    public User getConsumer() {
        return consumer;
    }
    public void setConsumer(User consumer) {
        this.consumer = consumer;
    }
}